﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using MxWeiXinPF.DBUtility;//Please add references
namespace MxWeiXinPF.DAL
{
	/// <summary>
	/// 数据访问类:mx_attachInfo
	/// </summary>
	public partial class mx_attachInfo
	{
		public mx_attachInfo()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("id", "mx_attachInfo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from mx_attachInfo");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(MxWeiXinPF.Model.mx_attachInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into mx_attachInfo(");
            strSql.Append("usedForCode,usedForName,refTableName,refTableId,attachName,fileSize,attachUri,flg,sort_id,createTime,remark)");
			strSql.Append(" values (");
            strSql.Append("@usedForCode,@usedForName,@refTableName,@refTableId,@attachName,@fileSize,@attachUri,@flg,@sort_id,@createTime,@remark)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
                    new SqlParameter("@usedForCode", SqlDbType.VarChar,50),
                    new SqlParameter("@usedForName", SqlDbType.VarChar,100),
					new SqlParameter("@refTableName", SqlDbType.VarChar,100),
					new SqlParameter("@refTableId", SqlDbType.Int,4),
					new SqlParameter("@attachName", SqlDbType.VarChar,100),
					new SqlParameter("@fileSize", SqlDbType.Int,4),
					new SqlParameter("@attachUri", SqlDbType.VarChar,500),
					new SqlParameter("@flg", SqlDbType.VarChar,50),
					new SqlParameter("@sort_id", SqlDbType.Int,4),
					new SqlParameter("@createTime", SqlDbType.DateTime),
					new SqlParameter("@remark", SqlDbType.VarChar,2000)};

            parameters[0].Value = model.usedForCode;
            parameters[1].Value = model.usedForName;
            parameters[2].Value = model.refTableName;
            parameters[3].Value = model.refTableId;
            parameters[4].Value = model.attachName;
            parameters[5].Value = model.fileSize;
            parameters[6].Value = model.attachUri;
            parameters[7].Value = model.flg;
            parameters[8].Value = model.sort_id;
            parameters[9].Value = model.createTime;
            parameters[10].Value = model.remark;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(MxWeiXinPF.Model.mx_attachInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update mx_attachInfo set ");

            strSql.Append("usedForCode=@usedForCode,");
            strSql.Append("usedForName=@usedForName,");

			strSql.Append("refTableName=@refTableName,");
			strSql.Append("refTableId=@refTableId,");
			strSql.Append("attachName=@attachName,");
			strSql.Append("fileSize=@fileSize,");
			strSql.Append("attachUri=@attachUri,");
			strSql.Append("flg=@flg,");
			strSql.Append("sort_id=@sort_id,");
			strSql.Append("createTime=@createTime,");
			strSql.Append("remark=@remark");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
                    new SqlParameter("@usedForCode", SqlDbType.VarChar,100),
                    new SqlParameter("@usedForName", SqlDbType.VarChar,100),

					new SqlParameter("@refTableName", SqlDbType.VarChar,100),
					new SqlParameter("@refTableId", SqlDbType.Int,4),
					new SqlParameter("@attachName", SqlDbType.VarChar,100),
					new SqlParameter("@fileSize", SqlDbType.Int,4),
					new SqlParameter("@attachUri", SqlDbType.VarChar,500),
					new SqlParameter("@flg", SqlDbType.VarChar,50),
					new SqlParameter("@sort_id", SqlDbType.Int,4),
					new SqlParameter("@createTime", SqlDbType.DateTime),
					new SqlParameter("@remark", SqlDbType.VarChar,2000),
					new SqlParameter("@id", SqlDbType.Int,4)};

            parameters[0].Value = model.usedForCode;
            parameters[1].Value = model.usedForName;
            parameters[2].Value = model.refTableName;
            parameters[3].Value = model.refTableId;
            parameters[4].Value = model.attachName;
            parameters[5].Value = model.fileSize;
            parameters[6].Value = model.attachUri;
            parameters[7].Value = model.flg;
            parameters[8].Value = model.sort_id;
            parameters[9].Value = model.createTime;
            parameters[10].Value = model.remark;
            parameters[11].Value = model.id;


			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from mx_attachInfo ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from mx_attachInfo ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public MxWeiXinPF.Model.mx_attachInfo GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select  top 1 id,usedForCode,usedForName,refTableName,refTableId,attachName,fileSize,attachUri,flg,sort_id,createTime,remark from mx_attachInfo ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			MxWeiXinPF.Model.mx_attachInfo model=new MxWeiXinPF.Model.mx_attachInfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public MxWeiXinPF.Model.mx_attachInfo DataRowToModel(DataRow row)
		{
			MxWeiXinPF.Model.mx_attachInfo model=new MxWeiXinPF.Model.mx_attachInfo();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=int.Parse(row["id"].ToString());
				}

                if (row["usedForCode"] != null)
                {
                    model.usedForCode = row["usedForCode"].ToString();
                }
                if (row["usedForName"] != null)
                {
                    model.usedForName = row["usedForName"].ToString();
                }

				if(row["refTableName"]!=null)
				{
					model.refTableName=row["refTableName"].ToString();
				}
				if(row["refTableId"]!=null && row["refTableId"].ToString()!="")
				{
					model.refTableId=int.Parse(row["refTableId"].ToString());
				}
				if(row["attachName"]!=null)
				{
					model.attachName=row["attachName"].ToString();
				}
				if(row["fileSize"]!=null && row["fileSize"].ToString()!="")
				{
					model.fileSize=int.Parse(row["fileSize"].ToString());
				}
				if(row["attachUri"]!=null)
				{
					model.attachUri=row["attachUri"].ToString();
				}
				if(row["flg"]!=null)
				{
					model.flg=row["flg"].ToString();
				}
				if(row["sort_id"]!=null && row["sort_id"].ToString()!="")
				{
					model.sort_id=int.Parse(row["sort_id"].ToString());
				}
				if(row["createTime"]!=null && row["createTime"].ToString()!="")
				{
					model.createTime=DateTime.Parse(row["createTime"].ToString());
				}
				if(row["remark"]!=null)
				{
					model.remark=row["remark"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select id,usedForCode,usedForName,refTableName,refTableId,attachName,fileSize,attachUri,flg,sort_id,createTime,remark ");
			strSql.Append(" FROM mx_attachInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
            strSql.Append(" id,usedForCode,usedForName,refTableName,refTableId,attachName,fileSize,attachUri,flg,sort_id,createTime,remark ");
			strSql.Append(" FROM mx_attachInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM mx_attachInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from mx_attachInfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

	 

		#endregion  BasicMethod
		#region  ExtensionMethod


        /// <summary>
        /// 删除一组数据
        /// </summary>
        public bool DeleteByRefTableInfo(string refTableName, string usedForCode, int refTableId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from mx_attachInfo ");
            strSql.Append(" where  refTableName=@refTableName and usedForCode=@usedForCode and  refTableId=@refTableId");
            SqlParameter[] parameters = {
                    new SqlParameter("@refTableName", SqlDbType.VarChar,100),
                     new SqlParameter("@usedForCode", SqlDbType.VarChar,50),
					new SqlParameter("@refTableId", SqlDbType.Int,4)
			};
            parameters[0].Value = refTableName;
            parameters[1].Value = usedForCode;
            parameters[2].Value = refTableId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



		#endregion  ExtensionMethod
	}
}

